import key from 'keymaster';

export default {
  namespace: 'count',
  state: {
    record: 0,
    current: 0,
  },
  reducers: {
    reducers_add(state) {
      console.log('reducers-add');
      const newCurrent = state.current + 1;
      return { ...state,
        record: newCurrent > state.record ? newCurrent : state.record,
        current: newCurrent,
      };
    },
    reducers_minus(state) {
      console.log('reducers-minus');
      return { ...state, current: state.current - 1};
    },
  },
  effects: {
    *add(action, { call, put }) {
      console.log('effects-add');
      yield call(delay, 1000);
      yield put({ type: 'reducers_add' });
    },
    *minus(action, { call, put }) {
      console.log('effects-minus');
      yield call(delay, 1000);
      yield put({ type: 'reducers_minus' });
    },
  },

  subscriptions: {
    keyboardWatcher({ dispatch }) {
      console.log('subscriptions-key');
      key('⌘+up, ctrl+up', () => { dispatch({type:'add'}) });
    },
  },
};

// ---------
// Helpers

function delay(timeout){
  return new Promise(resolve => {
    setTimeout(resolve, timeout);
  });
}
